package Controller1;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import Dao.orderDetailDao;
import Dao.paymentDao;
import Dao.sellorderDao;
import Model1.goods;
import Model1.orderDetail;
import Model1.payment;
import Model1.sellorder;
import Model1.sender;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.scene.control.Label;

import javafx.scene.control.RadioButton;

import javafx.scene.control.TableView;

import javafx.scene.control.TableColumn;

public class PaymentController implements Initializable{
	@FXML
	private TableView meal_order= new TableView<>();
	@FXML
	private TableColumn name;
	@FXML
	private TableColumn amount;
	@FXML
	private TableColumn price;
	@FXML
	private Button payDone;
	@FXML
	private RadioButton discount8;
	@FXML
	private RadioButton discount0;
	@FXML
	private RadioButton discount9;
	@FXML
	private RadioButton zhifubao;
	@FXML
	private RadioButton cash;
	@FXML
	private Label orderid;
	@FXML
	private Label total;
	@FXML
	private Label pay;
	@FXML
	private Label change;
	@FXML
	private TextField getPaid;
	@FXML
	public ToggleGroup tg1;
	@FXML
	public ToggleGroup tg2;
	public  static sender model = new sender();
	public void initialize(URL arg0, ResourceBundle arg1){
		tg1 = new ToggleGroup();
		discount8.setToggleGroup(tg1);
		discount9.setToggleGroup(tg1);
		discount0.setToggleGroup(tg1);
		discount0.setSelected(true); 
		
		tg2 = new ToggleGroup();
		cash.setToggleGroup(tg2);
		zhifubao.setToggleGroup(tg2);
		cash.setSelected(true); 
		
		orderid.setText(model.textProperty().get());
		sellorder so=sellorderDao.SearchById(model.textProperty().get());
		total.setText(so.getTotal()+"");
		pay.setText(so.getTotal()+"");
		
		List<orderDetail> detail=new ArrayList<>();
		detail=orderDetailDao.SearchByOrderId(model.textProperty().get());
		refresh(detail);
	}
	
	public void refresh(List<orderDetail> Mealtable1) {
		ObservableList<orderDetail> data = FXCollections.observableArrayList(Mealtable1);
		//Goods_id.setCellValueFactory(new PropertyValueFactory<>("id"));
		name.setCellValueFactory(new PropertyValueFactory<>("name"));
		amount.setCellValueFactory(new PropertyValueFactory<>("sales"));
		//Goods_kind.setCellValueFactory(new PropertyValueFactory<>("kind"));
		//Goods_inventory.setCellValueFactory(new PropertyValueFactory<>("inventory"));
		price.setCellValueFactory(new PropertyValueFactory<>("price"));
		//Goods_costPrice.setCellValueFactory(new PropertyValueFactory<>("costPrice"));
		//goods_id.setText("2");
		//goods_kind.setText("");
		//goods_inventory.setText("");
		//goods_price.setText("");
		//goods_name.setText("");
		//goods_costPrice.setText("");
	
		meal_order.setItems(data);
		meal_order.getSelectionModel().clearSelection();
		
	}
	
	public void discount8() {
		Float a=Float.parseFloat(total.getText().trim());
		pay.setText(a*0.8+"");
	}
	
	public void discount9() {
		Float a=Float.parseFloat(total.getText().trim());
		pay.setText(a*0.9+"");
	}
	
	public void discount0() {
		Float a=Float.parseFloat(total.getText().trim());
		pay.setText(a+"");
	}
	
	public void change() {
		float a=Float.parseFloat(getPaid.getText().trim());
		float b=Float.parseFloat(pay.getText().trim());
		if(a<b)
			change.setText(a-b+"");
		else
			change.setText((a-b)+"");
		
	}
	
	public void finish() {
		
		Calendar calendar= Calendar.getInstance();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		int now=0;
		String payId=paymentDao.SearchMaxId();
		if(payId!=null)
			now=Integer.parseInt(payId.trim())+1;
		else {
			now=1;
			
		}
		payId=now+"";
		float money=Float.parseFloat(pay.getText().trim());
		String method="";
		if(cash.isSelected())
			method+="现金";
		if(zhifubao.isSelected())
			method+="支付宝";
		double d=Double.parseDouble(change.getText().trim());
		if(d>=0) {
		payment pay=new payment(payId,money,orderid.getText(),method,dateFormat.format(calendar.getTime()));
		Boolean a=paymentDao.insert(pay);
		JOptionPane.showMessageDialog(null, "支付成功！"); 
		
		
		}
		else {
		JOptionPane.showMessageDialog(null, "支付失败，请重试！"); 
		}
	}
	
	public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View1/Payment.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.setResizable(false);
        primaryStage.show();
    }
	
	public void back() {
		Stage stage = (Stage)discount0.getScene().getWindow();
		stage.close();
		MainViewController mc = new MainViewController();
		try {
			mc.start(stage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
